package com.dragon.mmochat.message;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.MmochatMain;
import com.dragon.mmochat.model.battle.MmochatBattle;
import com.dragon.mmochat.model.enumType.MmochatServerType;
import com.dragon.mmochat.model.role.MmochatPet;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.model.role.MmochatRole;
import com.dragon.mmochat.model.skill.MmochatSkill;
import com.skymobi.buffer.SkymobiBuffer;
import com.skymobi.message.SkymobiProtocolMessage;

public class Mmochat_3_1S extends SkymobiProtocolMessage {
	int mapId;
	byte line;
	byte myPos;
	Byte petPos = null;
	boolean isAudience = false; // 是否是观战者
	MmochatBattle battle;

	public Mmochat_3_1S() {
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_S_3_1);
	}

	@Override
	protected void encodeBody(SkymobiBuffer out) {
		out.putInt(mapId);
		out.put(line);
		out.putInt(battle.getBattleId());
		out.putUnsigned((short) battle.getTurn());
		if (isAudience) {
			out.put((byte) 1);
		} else {
			out.put((byte) 0);
		}
		out.put(myPos);
		out.put((byte) battle.getRoles().size());
		for (Entry<Byte, MmochatRole> item : battle.getRoles().entrySet()) {
			byte pos = item.getKey();
			MmochatRole role = item.getValue();
			out.putInt(role.getId());
			out.put((byte) pos);
			out.putUnsignedShort(role.getBmpType().getValue());
			if (MmochatMain.serverType == MmochatServerType.跨服战服务器
					&& role instanceof MmochatPlayer) {
				MmochatPlayer p = (MmochatPlayer) role;
				String name = "";
				if (p.getCurChenHao() != null) {
					name = p.getCurChenHao().getName() + "-";
				}
				name += role.getName();
				out.putPrefixedString(name, 1);
			} else {
				out.putPrefixedString(role.getName(), 1);
			}
			out.putUnsignedInt(role.getSmeValue());
			out.putInt(role.getTotalBlood());
			out.putInt(role.getCurPkBlood());
			out.putInt(role.getTotalSprit());
			out.putInt(role.getCurPkSprit());
			// 技能
			if (pos == myPos || petPos != null && petPos == pos) {
				// 自己或自己的宠物
				List<MmochatSkill> attackSkills = new ArrayList<MmochatSkill>();
				List<MmochatSkill> limitSkills = new ArrayList<MmochatSkill>();
				List<MmochatSkill> assistSkills = new ArrayList<MmochatSkill>();
				for (MmochatSkill skill : role.getSkills().values()) {
					if (skill.getType().getSkillType() == MmochatSkill.攻击技能) {
						attackSkills.add(skill);
					} else if (skill.getType().getSkillType() == MmochatSkill.障碍技能) {
						limitSkills.add(skill);
					} else if (skill.getType().getSkillType() == MmochatSkill.辅助技能) {
						assistSkills.add(skill);
					}
				}
				// 法术技能
				if (attackSkills.size() == 0) {
					out.put((byte) 0);
				} else {
					out.put((byte) attackSkills.size());
					// 排序
					List<MmochatSkill> orderSkill = new ArrayList<MmochatSkill>(
							attackSkills);
					Collections.sort(orderSkill);
					for (MmochatSkill skill : orderSkill) {
						out.put((byte) skill.getType().getActionType()
								.getValue());
						out
								.putPrefixedString(
										skill.getType().toString()
												+ "("
												+ skill
														.getCostSprit(role instanceof MmochatPet)
												+ ")", 1);
						out.putInt(skill
								.getCostSprit(role instanceof MmochatPet));
					}
				}

				// 障碍技能
				if (limitSkills.size() == 0 || petPos != null && petPos == pos) {
					out.put((byte) 0);
				} else {
					out.put((byte) limitSkills.size());
					// 排序
					List<MmochatSkill> orderSkill = new ArrayList<MmochatSkill>(
							limitSkills);
					Collections.sort(orderSkill);
					for (MmochatSkill skill : orderSkill) {
						out.put((byte) skill.getType().getActionType()
								.getValue());
						out
								.putPrefixedString(
										skill.getType().toString()
												+ "("
												+ skill
														.getCostSprit(role instanceof MmochatPet)
												+ ")", 1);
						out.putInt(skill
								.getCostSprit(role instanceof MmochatPet));
					}
				}

				// 辅助技能
				if (assistSkills.size() == 0 || petPos != null && petPos == pos) {
					out.put((byte) 0);
				} else {
					out.put((byte) assistSkills.size());
					// 排序
					List<MmochatSkill> orderSkill = new ArrayList<MmochatSkill>(
							assistSkills);
					Collections.sort(orderSkill);
					for (MmochatSkill skill : orderSkill) {
						out.put((byte) skill.getType().getActionType()
								.getValue());
						out
								.putPrefixedString(
										skill.getType().toString()
												+ "("
												+ skill
														.getCostSprit(role instanceof MmochatPet)
												+ ")", 1);
						out.putInt(skill
								.getCostSprit(role instanceof MmochatPet));
					}
				}
			} else {
				// 其它人
				out.put((byte) 0);
				out.put((byte) 0);
				out.put((byte) 0);
			}
		}
	}

	public void setMapId(int mapId) {
		this.mapId = mapId;
	}

	public void setLine(byte line) {
		this.line = line;
	}

	public void setBattle(MmochatBattle battle) {
		this.battle = battle;
	}

	public void setMyPos(byte myPos) {
		this.myPos = myPos;
	}

	public void setPetPos(Byte petPos) {
		this.petPos = petPos;
	}

	public void setAudience(boolean isAudience) {
		this.isAudience = isAudience;
	}

}